package com.my.xinyidaixi.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.my.xinyidaixi.Globe.MyApplication;
import com.my.xinyidaixi.HttpVolley.Bitmap.FinalBitmap;
import com.my.xinyidaixi.Interface.Goods_interface;
import com.my.xinyidaixi.Model.GoodsItem;
import com.my.xinyidaixi.R;
import com.my.xinyidaixi.Util.CacheSave;

import java.util.ArrayList;

/**
 * Created by zw on 2017/8/6.
 */
public class xixieGoodAdapter extends BaseAdapter {
    private Context context= MyApplication.getInstance();
    private CacheSave cacheSave=new CacheSave();
    private ArrayList<GoodsItem> datalist;

    private LayoutInflater minflater;

    //private final DecimalFormat df;

    //接口回调  要实现adapter调用fragment中的方法 就要利用接口回调

    private Goods_interface goodsinterface;

    public void huidiao_Goods_interface(Goods_interface goodsinterface)
    {
        this.goodsinterface=goodsinterface;
    }

    //接口回调
    public xixieGoodAdapter(ArrayList<GoodsItem> datalist,Context context)
    {
        this.datalist = datalist;
        this.minflater=LayoutInflater.from(context);
        //df=new DecimalFormat("#.00");
    }
    @Override
    public int getCount()
    {
        if(datalist==null)
        {
            return 0;
        }
        return datalist.size();
    }

    @Override
    public Object getItem(int position)
    {
        return datalist.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ItemViewHolder holder=null;

        if(convertView==null)
        {
            convertView=minflater.inflate(R.layout.item_goods,parent,false);
            holder=new ItemViewHolder(convertView);
            convertView.setTag(holder);
        }

        else
        {
            holder= (ItemViewHolder) convertView.getTag();
        }
        GoodsItem item=datalist.get(position);
        holder.bindDate(item);

        return convertView;
    }

    class ItemViewHolder implements View.OnClickListener
    {

        private final RelativeLayout item_goods_layout;
        private final TextView item_goods_count;
        private final ImageView item_goods_img;
        private final TextView item_goods_text;
        private final TextView item_goods_value;
        private GoodsItem item;


        public ItemViewHolder(View itemview)
        {
            item_goods_layout = (RelativeLayout) itemview.findViewById(R.id.item_goods_layout);
            item_goods_count = (TextView) itemview.findViewById(R.id.item_goods_Count);
            item_goods_img = (ImageView) itemview.findViewById(R.id.item_goods_img);
            item_goods_text = (TextView) itemview.findViewById(R.id.item_goods_text);
            item_goods_value = (TextView) itemview.findViewById(R.id.item_goods_value);

            item_goods_layout.setOnClickListener(this);
        }

        public void bindDate(GoodsItem item)
        {
            this.item=item;
            item_goods_text.setText(item.name);
            item.count=goodsinterface.getSelectedItemCountById(item.id);
            item_goods_value.setText("￥"+String.valueOf(item.price));
            item_goods_count.setText(String.valueOf(item.count));

            FinalBitmap finalBitmap = FinalBitmap.create(context);
            finalBitmap.configBitmapLoadThreadSize(4);//定义线程数量
            finalBitmap.configDiskCacheSize(1024 * 1024 * 10);//设置缓存大小
            finalBitmap.configDiskCachePath(cacheSave.getCachePath()+ "/imageCache"); //设置缓存目录
            finalBitmap.display(item_goods_img,item.image_url.toString());

            if(item.count<1)
            {
                item_goods_count.setVisibility(View.GONE);
            }
            else
            {
                item_goods_count.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.item_goods_layout:
                {

                    int count=goodsinterface.getSelectedItemCountById(item.id);

                    goodsinterface.add(item);

                    count++;

                    item_goods_count.setText(String.valueOf(count));
                    break;

                }
                default:
                    break;
            }
        }
    }
}
